import { ACCESS_NUM } from "@/access/accessNum";
import { getLoginUserUsingGet } from "@/api/userController";
import { nanoid } from "nanoid";
import { defineStore } from "pinia";
import { ref } from "vue";
export const useLoginUserStore = defineStore("loginUserStore", () => {

  const loginUser = ref<API.LoginUserVO>({
    userName: '未登录'
  })

  // 设置登录用户信息
  const setLoginUser = (newloginUser: API.LoginUserVO) => {
    loginUser.value = newloginUser
  }

  // 获取用户信息
  const fetchLoginUser = async () => {
    const res = await getLoginUserUsingGet()
    if(res.data.code === 0 && res.data.data) {
      loginUser.value = res.data.data
    } else {
      // 登录失败, 修改loginUser为notLogin,代表执行过登录,只是失败了,不需要执行再自动登录了
      loginUser.value = { userRole: ACCESS_NUM.NOT_LOGIN }
      // setTimeout(() => {
      //   loginUser.value = { userRole: ACCESS_NUM.ADMIN, userName: 'admin', id: nanoid() as unknown as number }
      // }, 3000);
    }
  }

  return {
    loginUser,
    setLoginUser,
    fetchLoginUser
  }

})

